{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Move Zeroes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #two-pointers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #双指针"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: moveZeroes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #移动零"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定一个数组 <code>nums</code>，编写一个函数将所有 <code>0</code> 移动到数组的末尾，同时保持非零元素的相对顺序。</p>\n",
    "\n",
    "<p><strong>请注意</strong>&nbsp;，必须在不复制数组的情况下原地对数组进行操作。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> nums = <code>[0,1,0,3,12]</code>\n",
    "<strong>输出:</strong> <code>[1,3,12,0,0]</code>\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> nums = <code>[0]</code>\n",
    "<strong>输出:</strong> <code>[0]</code></pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示</strong>:</p>\n",
    "<meta charset=\"UTF-8\" />\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>\n",
    "\t<li><code>-2<sup>31</sup>&nbsp;&lt;= nums[i] &lt;= 2<sup>31</sup>&nbsp;- 1</code></li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><b>进阶：</b>你能尽量减少完成的操作次数吗？</p>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [move-zeroes](https://leetcode.cn/problems/move-zeroes/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [move-zeroes](https://leetcode.cn/problems/move-zeroes/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[0,1,0,3,12]', '[0]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def moveZeroes(self, nums: List[int]) -> None:\n",
    "        index = 0  \n",
    "       \n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] != 0:\n",
    "                # If the current element is non-zero, swap it with the element at index\n",
    "                # This effectively moves non-zero elements to the beginning of the list\n",
    "                nums[i], nums[index] = nums[index], nums[i]\n",
    "                index += 1 \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def moveZeroes(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: void Do not return anything, modify nums in-place instead.\n",
    "        \"\"\"\n",
    "        n = len(nums)\n",
    "        j = 0\n",
    "        for i in range(n):\n",
    "            if nums[i]:\n",
    "                nums[j] = nums[i]\n",
    "                j += 1\n",
    "        while j < n:\n",
    "            nums[j] = 0\n",
    "            j += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def moveZeroes(self, nums: 'List[int]') -> 'None':\n",
    "        \"\"\"\n",
    "        Do not return anything, modify nums in-place instead.\n",
    "        \"\"\"\n",
    "        nums.sort(key=bool, reverse=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def moveZeroes(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: void Do not return anything, modify nums in-place instead.\n",
    "        \"\"\"\n",
    "        \n",
    "      \n",
    "        lens=len(nums)\n",
    "        dict=collections.Counter(nums)\n",
    "        v=dict[0]       \n",
    "        for i in range(v):\n",
    "            nums.remove(0)       \n",
    "        for i in range(v):\n",
    "            nums.append(0)\n",
    "        \n",
    "        # 10%\n",
    "        # for i in nums:\n",
    "        #     if i == 0:\n",
    "        #          nums.remove(i)\n",
    "        #          nums.append(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def moveZeroes(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: void Do not return anything, modify nums in-place instead.\n",
    "        \"\"\"\n",
    "        count=0\n",
    "        for i in range(len(nums)):\n",
    "            if not nums[count] ^ 0:\n",
    "                nums.pop(count)\n",
    "                nums.append(0)\n",
    "                count-=1\n",
    "            count+=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def moveZeroes(self, nums: List[int]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify nums in-place instead.\n",
    "        \"\"\"\n",
    "        \n",
    "        i = 0\n",
    "        for num in nums:\n",
    "            if num != 0:\n",
    "                nums[i] = num\n",
    "                i += 1\n",
    "        for j in range(i,len(nums)):\n",
    "            nums[j] = 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def moveZeroes(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: void Do not return anything, modify nums in-place instead.\n",
    "        \"\"\"\n",
    "        p = 0\n",
    "        while p < len(nums) and set(nums[p:]) != {0}:\n",
    "            if nums[p] == 0:\n",
    "                nums.pop(p)\n",
    "                nums.append(0)\n",
    "            else:\n",
    "                p += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def moveZeroes(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: void Do not return anything, modify nums in-place instead.\n",
    "        \"\"\"\n",
    "        # for i in range(nums.count(0)):\n",
    "        #     nums.remove(0)\n",
    "        #     nums.append(0)\n",
    "        # nums[:] = nums\n",
    "        if len(nums) <= 1:\n",
    "            return \n",
    "        left = 0\n",
    "        zero_length = 0\n",
    "        while left < len(nums) :\n",
    "            if nums[left] == 0:\n",
    "                zero_length += 1\n",
    "                left += 1\n",
    "                continue\n",
    "            if zero_length == 0:\n",
    "                left += 1\n",
    "                continue\n",
    "            print(nums[left])\n",
    "            nums[left-zero_length] = nums[left]\n",
    "            nums[left] = 0\n",
    "            left += 1\n",
    "            print(left,zero_length)\n",
    "        print(nums)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def moveZeroes(self, nums: List[int]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify nums in-place instead.\n",
    "        \"\"\"\n",
    "        i = j = 0\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] != 0:\n",
    "                temp = nums[j]\n",
    "                nums[j] = nums[i]\n",
    "                nums[i] = temp\n",
    "                j += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def moveZeroes(self, nums: List[int]) -> None:\r\n",
    "        \"\"\"\r\n",
    "        Do not return anything, modify nums in-place instead.\r\n",
    "        \"\"\"\r\n",
    "        index = []\r\n",
    "        for idx, value in enumerate(nums):\r\n",
    "            if value == 0:\r\n",
    "                index.append(idx)\r\n",
    "        \r\n",
    "        index[:] = index[::-1]\r\n",
    "        for idx in index:\r\n",
    "            nums[idx:len(nums)-1] = nums[idx+1:]\r\n",
    "            nums[-1] = 0\r\n",
    "        \r\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def moveZeroes(self, nums: List[int]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify nums in-place instead.\n",
    "        \"\"\"\n",
    "        number = nums.count(0)\n",
    "        for i in range(number):\n",
    "            nums.remove(0)\n",
    "        for i in range(number):\n",
    "            nums.append(0)\n",
    "        \n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def moveZeroes(self, nums: List[int]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify nums in-place instead.\n",
    "        \"\"\"\n",
    "        i=j=0\n",
    "        while i<len(nums):\n",
    "            if nums[i]!=0:\n",
    "                nums[i],nums[j]=nums[j],nums[i]\n",
    "                j=j+1\n",
    "            i=i+1  \n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def moveZeroes(self, nums: List[int]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify nums in-place instead.\n",
    "        \"\"\"\n",
    "        for i in range(nums.count(0)):\n",
    "            nums.remove(0)\n",
    "            nums.append(0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def moveZeroes(self, nums: List[int]) -> None:\r\n",
    "        \"\"\"\r\n",
    "        Do not return anything, modify nums in-place instead.\r\n",
    "        \"\"\"\r\n",
    "        if not nums:\r\n",
    "            return\r\n",
    "        p = 0\r\n",
    "        l = 0\r\n",
    "        while p < len(nums):\r\n",
    "            if nums[p] != 0:\r\n",
    "                nums[l], nums[p] = nums[p], nums[l]\r\n",
    "                l += 1\r\n",
    "            p += 1\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def moveZeroes(self, nums: List[int]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify nums in-place instead.\n",
    "        \"\"\"\n",
    "        i = 0\n",
    "        while i < len(nums):\n",
    "            if nums[i] == 0:\n",
    "                nums.pop(i)\n",
    "                nums.append(0)\n",
    "                if nums[i] == 0:\n",
    "                    if nums[i::] == [0 for j in range(len(nums) - i)]:\n",
    "                        break\n",
    "                    i = i - 1\n",
    "                    \n",
    "            i = i + 1\n",
    "        return nums\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def moveZeroes(self, nums: List[int]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify nums in-place instead.\n",
    "        \"\"\"\n",
    "        \n",
    "        zero_nms = nums.count(0)\n",
    "        if zero_nms == 0:\n",
    "            return nums\n",
    "        else:\n",
    "            while set(nums[-1:-(zero_nms+1):-1]) != {0}:\n",
    "                for i in range(len(nums)):\n",
    "                    if nums[i] == 0 and set(nums[i+1:]) != {0}:\n",
    "                        nums[i:-1], nums[-1] = nums[i+1:],0\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def moveZeroes(self, nums: List[int]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify nums in-place instead.\n",
    "        \"\"\"\n",
    "        count = 0\n",
    "        for i in range(len(nums)-1, -1, -1):\n",
    "            if nums[i] == 0:\n",
    "                del nums[i]\n",
    "                nums.append(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def moveZeroes(self, nums: List[int]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify nums in-place instead.\n",
    "        \"\"\"\n",
    "        j = 0\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] != 0:\n",
    "                continue\n",
    "            if j < i:\n",
    "                j = i\n",
    "            while j < len(nums) - 1 and nums[j] == 0:\n",
    "                j += 1\n",
    "            tmp = nums[i]\n",
    "            nums[i] = nums[j]\n",
    "            nums[j] = tmp\n",
    "        return nums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def moveZeroes(self, nums: List[int]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify nums in-place instead.\n",
    "        \"\"\"\n",
    "        for i in nums:\n",
    "            if i == 0:\n",
    "                nums.remove(0)\n",
    "                nums.append(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def moveZeroes(self, nums: List[int]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify nums in-place instead.\n",
    "        \"\"\"\n",
    "        k = 0\n",
    "        for i, n in enumerate(nums):\n",
    "            if n != 0:\n",
    "                nums[k], nums[i] = nums[i], nums[k]\n",
    "                k += 1\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def moveZeroes(self, nums: List[int]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify nums in-place instead.\n",
    "        \"\"\"\n",
    "        \n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            swaped = False\n",
    "            for j in range(n-1-i):\n",
    "                if nums[j] == 0:\n",
    "                    # print(\"awe\")\n",
    "                    nums[j], nums[j+1] = nums[j+1], nums[j]\n",
    "                    swaped = True\n",
    "            if swaped is False:\n",
    "                break\n",
    "        # print(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def moveZeroes(self, nums: List[int]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify nums in-place instead.\n",
    "        \"\"\"\n",
    "        p = 0\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] != 0:\n",
    "                nums[i], nums[p] = nums[p], nums[i]\n",
    "                p += 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def moveZeroes(self, nums: List[int]) -> None:\n",
    "        slow, fast = 0, 0\n",
    "        while fast<len(nums):\n",
    "            if nums[fast] != 0:\n",
    "                nums[slow]=nums[fast]\n",
    "                slow +=1\n",
    "            fast += 1\n",
    "        for i in range(slow, len(nums)):\n",
    "            nums[i]=0\n",
    "        \n",
    "\n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def moveZeroes(self, nums: List[int]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify nums in-place instead.\n",
    "        \"\"\"\n",
    "        i = cnt = 0\n",
    "        length = len(nums)\n",
    "        while i < length:\n",
    "            if nums[i] == 0 and i < length - cnt:\n",
    "                nums.pop(i)\n",
    "                nums.append(0)\n",
    "                cnt += 1\n",
    "            else:\n",
    "                i += 1\n",
    "        print(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def moveZeroes(self, nums: List[int]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify nums in-place instead.\n",
    "        \"\"\"\n",
    "        slow = 0\n",
    "        fast = 0\n",
    "        length = len(nums)\n",
    "\n",
    "        while fast < length:\n",
    "            if nums[fast] != 0:\n",
    "                nums[slow] = nums[fast]\n",
    "                slow = slow + 1\n",
    "            fast = fast + 1\n",
    "\n",
    "        while slow  < length:\n",
    "            nums[slow ] =0\n",
    "            slow = slow + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def moveZeroes(self, nums: List[int]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify nums in-place instead.\n",
    "        \"\"\"\n",
    "        i = 0\n",
    "        j = len(nums)\n",
    "        while(i<j):\n",
    "            if nums[i] == 0:\n",
    "                del(nums[i])\n",
    "                nums.append(0)\n",
    "                i-=1\n",
    "                j-=1\n",
    "            i+=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def moveZeroes(self, nums: List[int]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify nums in-place instead.\n",
    "        \"\"\"\n",
    "        i = 0\n",
    "        j = len(nums)\n",
    "        while(i<j):\n",
    "            if nums[i] == 0:\n",
    "                del(nums[i])\n",
    "                nums.append(0)\n",
    "                i-=1\n",
    "                j-=1\n",
    "            i+=1"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
